package com.spc.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.spc.constant.MessageConstant;
import com.spc.entity.PageResult;
import com.spc.entity.QueryPageBean;
import com.spc.entity.Result;
import com.spc.pojo.QuestionBank;
import com.spc.service.QuestionBankService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述:
 * 题库
 *
 * @author 景远超
 * @create 2020-05-16 19:20
 */
@RestController
@RequestMapping("/bank")
public class QuestionBankController {
    @Reference
    private QuestionBankService questionBankService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = questionBankService.findPage(queryPageBean);

        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            QuestionBank questionBank = questionBankService.findById(id);
            return new Result(true, MessageConstant.GET_QUESTION_DETAILE_REPORT_SUCCESS, questionBank);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_QUESTION_DETAILE_REPORT_FAIL);
        }
    }

    /**
     * 题库数据修改
     * @param questionBank
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody QuestionBank questionBank){
        try {
            questionBankService.edit(questionBank);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_QUESTION_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_QUESTION_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            questionBankService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_QUESTION_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_QUESTION_SUCCESS);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody QuestionBank questionBank){
        try {
            questionBankService.add(questionBank);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_QUESTION_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_QUESTION_SUCCESS);
    }
}
